package com.simtop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.simtop.aop.SystemServiceLog;
import com.simtop.domain.experiment.ExperimentProject;
import com.simtop.domain.experiment.ExperimentProjectVo;
import com.simtop.exception.CustomException;
import com.simtop.mapper.ExperimentProjectMapper;
import com.simtop.service.ExperimentProjectService;
import com.simtop.utils.FileUploadAndDownload;
import com.simtop.utils.ifnull.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author lvyongwei
 */
@Service
public class ExperimentProjectServiceImpl implements ExperimentProjectService {

    @Autowired
    private ExperimentProjectMapper experimentProjectMapper;

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    /**
     * 添加高校虚拟仿真建设
     * @param experimentProject
     * @return
     */
    @Override
    @SystemServiceLog(description = "添加高校虚拟仿真建设内容")
    public boolean insertExperimentProject(ExperimentProject experimentProject,MultipartFile photo,
                                           MultipartFile video) {
        String experimentPhoto = fileUploadAndDownload.upload2(photo, "experiment");
        if(StringUtils.isEmpty(experimentPhoto)){
            throw new CustomException("封面上传失败");
        }
        experimentProject.setExperimentPhoto(experimentPhoto);
        String experimentVideo = fileUploadAndDownload.upload2(video, "experiment");
        if(StringUtils.isEmpty(experimentVideo)){
            throw new CustomException("项目简介视频上传失败");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name",video.getOriginalFilename());
        jsonObject.put("path",experimentVideo);
        experimentProject.setExperimentVideo(jsonObject.toString());
        return experimentProjectMapper.insertExperimentProject(experimentProject);
    }

    /**
     * 查询信息
     * @param experimentProject
     * @return
     */
    @Override
    public List<ExperimentProjectVo> getExperimentProject(ExperimentProject experimentProject) {
        return experimentProjectMapper.getExperimentProject(experimentProject);
    }

    /**
     * 修改信息
     * @param experimentProject
     * @return
     */
    @Override
    @SystemServiceLog(description = "修改高校虚拟仿真建设内容")
    public boolean updateExperimentProject(ExperimentProject experimentProject,MultipartFile photo,
                                           MultipartFile video) {
        if(photo != null && photo.getOriginalFilename().length() != 0){
            String experimentPhoto = fileUploadAndDownload.upload2(photo, "experiment");
            if(StringUtils.isEmpty(experimentPhoto)){
                throw new CustomException("封面上传失败");
            }
            experimentProject.setExperimentPhoto(experimentPhoto);
        }
        if(video != null && photo.getOriginalFilename().length() != 0){
            String experimentVideo = fileUploadAndDownload.upload2(video, "experiment");
            if(StringUtils.isEmpty(experimentVideo)){
                throw new CustomException("项目简介视频上传失败");
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name",video.getOriginalFilename());
            jsonObject.put("path",experimentVideo);
            experimentProject.setExperimentVideo(jsonObject.toString());
        }
        return experimentProjectMapper.updateExperimentProject(experimentProject);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @SystemServiceLog(description = "删除高校虚拟仿真建设内容")
    public boolean deleteExperimentProject(List<Integer> ids) {
        return experimentProjectMapper.deleteExperimentProject(ids);
    }

    /**
     * 根据id获取虚拟仿真建设内容
     * @param experimentId
     * @return
     */
    @Override
    public ExperimentProject getExperimentProjectById(Integer experimentId) {
        return experimentProjectMapper.getExperimentProjectById(experimentId);
    }
}
